package hy;

import hy.beans.Course;
import hy.beans.Teacher;
import hy.beans.User;
import hy.dao.*;
import hy.utils.Util;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class App {
    SqlSession session;;
    @Before
    public void init(){
        session = Util.getSession();
    }
    @After
    public void close(){
        // 增删改执行之后必须提交
        session.commit();
        session.close();
    }

    @Test
    public void m1(){
        ICourseDao dao = session.getMapper(ICourseDao.class);
        System.out.println(dao.selectStu());
    }
    @Test
    public void m2(){
        ITeacherDao dao = session.getMapper(ITeacherDao.class);
        System.out.println(dao.selectTeacher());
    }

    @Test
    public void m3(){
        ICourseDao dao = session.getMapper(ICourseDao.class);
        System.out.println(dao.selectCourse());
    }

    @Test
    public void m4(){
        IEquipmentDao dao = session.getMapper(IEquipmentDao.class);
        System.out.println(dao.selectAllEquipment());
    }
    @Test
    public void m5(){
        ICourseDao dao = session.getMapper(ICourseDao.class);
            dao.selectCourse2();
//        System.out.println(dao.selectStuByCourseId(1));
    }

    @Test
    public void select4() throws InterruptedException {
        ICourseDao dao = session.getMapper(ICourseDao.class);
        List<Course> list = dao.selectCourse2();
        System.out.println(list.get(0).getName());
//        //观察控制台sql执行情况，并没有执行第二条sql,下面用到了数据才执行并加载
//        Thread.sleep(5000);
        list.remove(0);
        System.out.println(list.get(1).getList());

    }

    @Test
    public void m6(){
        ICourseType dao = session.getMapper(ICourseType.class);
        System.out.println(dao.selectAllTypeById(null));
    }

    @Test
    public void m7(){
        IUserDao dao = session.getMapper(IUserDao.class);
        System.out.println(dao.selectUser("abc","137"));
    }
    @Test
    public void m8(){
        IUserDao dao = session.getMapper(IUserDao.class);
        System.out.println(dao.selectMultiColumn("","137"));
    }

    @Test
    public void m9(){
        IUserDao dao = session.getMapper(IUserDao.class);
        System.out.println(dao.updateUser(null,"123456",1));
    }

    @Test
    public void m10(){
        IUserDao dao = session.getMapper(IUserDao.class);
        System.out.println(dao.selectByIds(1,2,3,4));
    }
    @Test
    public void m11(){
        IUserDao dao = session.getMapper(IUserDao.class);
        ArrayList arr = new ArrayList();
        arr.add(new User(null,"a1","123","13999"));
        arr.add(new User(null,"a2","12223","1222999"));
        System.out.println(dao.insertUserBatch(arr));
    }
    @Test
    public void m12(){
        IUserDao dao = session.getMapper(IUserDao.class);
        ArrayList arr = new ArrayList();
        arr.add(new User(3,"a3","333","13333"));
        arr.add(new User(4,"a4","444","1444"));
        System.out.println(dao.updateBatch(arr));
    }

    @Test
    public void oneLevel()throws Exception{
        ITeacherDao dao = session.getMapper(ITeacherDao.class);
        System.out.println(dao.selectById(1));
        Thread.sleep(5000);
//        session.clearCache();//会清空一级缓存
        //增删改会清空一二级缓存
//        dao.insertTeacher(new Teacher());
//        dao = Util.getSession().getMapper(ITeacherDao.class);
        //参数跟上一次必须相同才命中
        System.out.println(dao.selectById(2));
    }


    @Test
    public  void twoLevel() throws Exception {
        for(int i=0;i<3;i++){
            new Thread("线程"+i){
                public void run() {
                    SqlSession s=Util.getSession();
                    ITeacherDao dao=s.getMapper(ITeacherDao.class);
                    Teacher t = dao.selectById(1);
                    s.commit();//必须提交，否则不会把数据存入到session缓存中.
                    String tn = Thread.currentThread().getName();
                    if(tn.equals("线程0")){
                        //修改了数据，如果readonly=false,则不会影响缓存中数据，因为是反序列化出的实例，
                        //如果为true，则缓存中实例跟t是同一个实例，一起受响。
                        t.setName("hello");
                    }
                    System.out.println(tn+":"+t);
                }
            }.start();

            Thread.sleep(2000);//要成功写入到缓存需要一定时间。
            //会清除缓存，当i=1时再从数据库查数据并写缓存。
            if(i==0){
                SqlSession s=Util.getSession();
                ITeacherDao dao=s.getMapper(ITeacherDao.class);
                dao.insertTeacher(new Teacher());//增删改后会清空二级缓存,导致只有最后一个线程命中
                s.commit();
            }
        }
    }
}
